package com.cwt_taxi_xa;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.cwt_taxi_xa.constants.Constants;
import com.cwt_taxi_xa.db.bll.MessageBiz;
import com.cwt_taxi_xa.model.MessageCenter;

import java.util.ArrayList;
import java.util.List;

/**
 * 收信箱信息界面
 *
 * @author ferch
 *
 */
public class MessageList extends CWTBaseActivity implements OnItemClickListener,
		OnScrollListener,OnTouchListener {
	private static final String TAG = "MessageList.";
	public TextView tips,tv_show_setting;
	public ListView message_listView;
	public List<MessageCenter> list;
	public int currentPage = 1;// 默认第一页
	public static final int lineSize = 5;// 每次显示4条
	public int pageSize = 1; // 总多少页
	public int allRecord = 0;// 总记录
	private int lastItem;
	public View view;// 底部视图，加载更多
	TextView loadmore;
	public MessageAdapter messageAdapter;
	public static MessageList instance;
	private MessageBiz biz;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.message_list);

		biz = new MessageBiz(this);
		view = getLayoutInflater().inflate(R.layout.loadmore, null);
		tips = (TextView) this.findViewById(R.id.q_tip);
		tv_show_setting = (TextView) this.findViewById(R.id.tv_show_setting);
		message_listView = (ListView) this
				.findViewById(R.id.message_listView);
		message_listView.addFooterView(view);
		message_listView.setOnScrollListener(this);
		instance = MessageList.this;
		// 获取总记录
		allRecord = biz.getMessageCenterCount();
		queryMessageCenter();
		messageAdapter = new MessageAdapter(this);
		message_listView.setOnItemClickListener(this);
		// 获取总页数
		pageSize = (allRecord + lineSize - 1) / lineSize;
		loadmore = (TextView)view.findViewById(R.id.moreText);
		message_listView.setOnTouchListener(this);
		tips.setOnTouchListener(this);
		tv_show_setting.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(MessageList.this, MessageSetting.class);
				intent.putExtra("from", 3);
				startActivity(intent);
			}
		});
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	/**
	 * 查找第一次的数据
	 */
	public void queryMessageCenter() {
		new AsyncTask<String, Integer, Long>() {
			@Override
			protected Long doInBackground(String... params) {
				List<MessageCenter> listCache = biz.findMessageCenter(currentPage, lineSize);
				if(listCache != null && listCache.size() != 0){
					if(list == null){
						list = new ArrayList<MessageCenter>();
					}
					list.addAll(listCache);
				}
				return 1L;
			}

			@Override
			protected void onPostExecute(Long result) {
				if (list != null && list.size() > 0) {
					tips.setVisibility(View.GONE);
					messageAdapter.setData(list);
					if(currentPage == 1){
						message_listView.setAdapter(messageAdapter);
					}else{
						messageAdapter.notifyDataSetChanged();
					}

					//把未读信息变为已读
					//						biz.updateMessageCenter();

					if (currentPage == pageSize) {
						message_listView.removeFooterView(view);
					}else{
						loadmore.setText(Constants.LOAD_MORE);
					}
				} else {
					tips.setText("暂无文本信息!");
					tips.setVisibility(View.VISIBLE);
				}
			}
		}.execute();
	}
	/**
	 * 各个item点击事件
	 */
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		MessageCenter info = list.get(arg2);
		if(info.getIsread() == 0){
			biz.updateMessageCenter(info.getId());
		}
		Intent intent = new  Intent();
		//跳转到详细页面
		intent.setClass(MessageList.this, MessageActivity.class);
		//把id传到详细页面，具体接受参照InvidationDetail，
		intent.putExtra("message", info.getContent());
		startActivity(intent);
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
						 int visibleItemCount, int totalItemCount) {
		lastItem = firstVisibleItem + visibleItemCount - 1; // 统计是否到最后
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		if (lastItem == messageAdapter.getCount() && currentPage < pageSize
				&& scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
			currentPage++;
			// 设置显示位置
			loadmore.setText(Constants.LOADING);
			message_listView.setSelection(lastItem);
			// 增加数据
			queryMessageCenter();
		}

	}

	/*class MyThread implements Runnable {
		@Override
		public void run() {

			while (timeta > 0) {

				if (timeta == 0) {
					break;
				}

				timeta--;
				Message message = new Message();
				message.what = 102;
				Bundle bl = new Bundle();
				bl.putInt("step", timeta);
				message.setData(bl);
				handler_refresh.sendMessage(message);
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					AppLog.e(ExceptionUtil.getInfo(e), e);
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}*/

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		return false;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void setStatus(int index, boolean status) {
		// TODO Auto-generated method stub

	}

	@Override
	public void sendMsg(int type, Object obj) {
		// TODO Auto-generated method stub

	}
}
